<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
  const arr1 = [5,6,7];
  const arr2 = [3, 4];

  const foo = function (a, b) {

    for (let i = 0, len = b.length; i < len; i++) {
      for (let j = 0, leng = a.length; j < leng; j++) {
        if (a[j] > b[i] ) {
          a.splice(0, 0, b[i]);
        }else if(a[j] < b[i] && !a[j+1] || b[i] < a[j+1]){
          a.splice(j,0,b[i]);
        }
      }
    }
    return a;
  };

  console.log(foo(arr1, arr2));
</script>
</body>
</html>